<template>
  <div class="container">
    <div class="app-container">
      <div class="search">
        <el-form class="search-form">
          <el-form-item label="工单编号：" label-width="100px">
            <el-input
              v-model="pageParams.taskCode"
              placeholder="请输入"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item label="工单状态：" label-width="100px">
            <el-select v-model="pageParams.status" placeholder="请选择">
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.statusName"
                :value="item.statusId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              class="search-btn"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="searchTask"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="list">
        <div class="btns">
          <el-button
            type="primary"
            class="add-btn"
            icon="el-icon-circle-plus-outline"
            @click="showAddDialog = true"
            >新建</el-button
          >
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column width="80px" label="序号">
            <template v-slot="{ row }"> {{ row.index }} </template>
          </el-table-column>
          <el-table-column prop="taskCode" label="工单编号"> </el-table-column>
          <el-table-column prop="innerCode" label="设备编号"> </el-table-column>
          <el-table-column prop="taskType.typeName" label="工单类型">
          </el-table-column>
          <el-table-column label="工单方式">
            <span>手动</span>
          </el-table-column>
          <el-table-column label="工单状态"
            ><template v-slot="{ row }">{{
              row.taskStatus === 1
                ? "待办"
                : row.taskStatus === 2
                ? "进行"
                : row.taskStatus === 3
                ? "取消"
                : "完成"
            }}</template>
          </el-table-column>
          <el-table-column prop="userName" label="运营人员"> </el-table-column>
          <el-table-column label="创建日期" width="200px"
            ><template v-slot="{ row }">{{
              row.createTime.split("T").join(" ")
            }}</template></el-table-column
          >
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button type="text" @click="getTaskId(row.taskId)"
                >查看详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="space-between" style="margin-top: 20px">
          <span class="pageCount"
            >共{{ pageParams.totalCount }}条记录 第{{ pageParams.pageIndex }}/{{
              pageParams.totalPage
            }}页</span
          >
          <el-pagination
            layout="slot"
            :current-page.sync="pageParams.pageIndex"
            :page-size="pageParams.pagesize"
            :total="pageParams.totalPage"
          >
            <el-button
              type="primary"
              :disabled="pageParams.pageIndex === 1 ? true : false"
              class="pagi-btn"
              :class="{ disabled: pageParams.pageIndex === 1 }"
              @click="goPrev"
              >上一页</el-button
            >
            <el-button
              type="primary"
              :disabled="
                pageParams.pageIndex === pageParams.totalPage ? true : false
              "
              :class="{
                disabled: pageParams.pageIndex === pageParams.totalPage,
              }"
              class="pagi-btn"
              @click="goNext"
              >下一页</el-button
            >
          </el-pagination>
        </el-row>
      </div>
      <!-- 弹窗 -->
      <addoperation
        v-if="showAddDialog"
        :isShow.sync="showAddDialog"
        @searchTask="searchTask"
      />
      <getInfo
        v-if="showInfoDialog"
        :taskId="taskId"
        :isShow.sync="showInfoDialog"
        @searchTask="searchTask"
        @openAdd="openAdd"
      />
    </div>
  </div>
</template>
<script>
import addoperation from "@/views/task/components/addoperation.vue";
import getInfo from "@/views/task/components/getInfo.vue";
import { getAllTaskStatusApi, searchTaskApi } from "@/api/task";
export default {
  name: "Business",
  components: {
    addoperation,
    getInfo,
  },
  data() {
    return {
      status: [],
      tableData: [],
      taskId: "",
      showAddDialog: false,
      showSetDialog: false,
      showInfoDialog: false,
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        totalCount: 0,
        taskCode: "",
        status: "",
        isRepair: true,
      },
    };
  },
  created() {
    this.getTaskStatus();
    this.searchTask();
  },
  methods: {
    // 获取工单状态
    async getTaskStatus() {
      const res = await getAllTaskStatusApi();
      this.status = { ...res };
    },
    // 搜索工单
    async searchTask() {
      const res = await searchTaskApi({
        ...this.pageParams,
      });
      this.tableData = [...res.currentPageRecords];
      console.log(this.tableData);
      this.pageParams.pageIndex = +res.pageIndex;
      this.pageParams.pageSize = +res.pageSize;
      this.pageParams.totalPage = +res.totalPage;
      this.pageParams.totalCount = +res.totalCount;

      // 序号
      let pageNow = this.pageParams.pageIndex;
      this.tableData.forEach((item, index) => {
        this.$set(
          item,
          "index",
          pageNow > 1 ? (pageNow - 1) * 10 + index + 1 : index + 1
        );
      });
    },

    getTaskId(id) {
      this.taskId = id;
      this.showInfoDialog = true;
    },

    openAdd() {
      this.showAddDialog = true;
    },
    // 上一页
    goPrev() {
      this.pageParams.pageIndex--;
      this.searchTask();
    },
    // 下一页
    goNext() {
      this.pageParams.pageIndex++;
      this.searchTask();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #f8fafd;
  padding: 20px;
  .search {
    background-color: #fff;
    display: flex;
    height: 64px;
    align-items: center;
    padding-top: 23px;
    margin-bottom: 20px;
    .search-form {
      display: flex;
      align-items: center;
      .search-btn {
        background-color: #5f84ff;
        margin-left: 15px;
        font-size: 16px;
        height: 36px;
        width: 80px;
        text-align: center;
        padding: 0;
      }
    }
  }
  .list {
    // height: 673px;
    padding: 20px;
    background-color: #fff;

    .btns {
      margin-bottom: 15px;
    }
    .add-btn {
      width: 80px;
      text-align: center;
      padding: 12px 0px;
      background-color: #ff8035;
      border: none;
      font-size: 14px;
    }
    .setting-btn {
      border: none;
      background-color: #fbf4f0;
      color: #716762;
      width: 80px;
      text-align: center;
      padding: 12px 0px;
    }
    .pageCount {
      color: #dbdfe5;
      margin-left: 10px;
    }
  }
  .pagi-btn {
    background-color: #d5ddf8;
    color: #606266;
    width: 70px;
    height: 32px;
    text-align: center;
  }
  .disabled {
    background-color: #eceff8;
    color: #d2d7dd;
  }
}
</style>
